﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Admx_Generator
{
    internal partial class FrmAddLanguage : Form
    {
        internal FrmAddLanguage(List<AdmlFile> excludeLanguages)
        {
            InitializeComponent();

            System.Threading.Tasks.Task FillComboBox = new System.Threading.Tasks.Task(() => { this.SetUI(excludeLanguages); });
            FillComboBox.Start();
        }

        #region (Properties)

        /// <summary>
        /// Gets or Sets the selected culture
        /// </summary>
        internal culture SelectedCulture { get; private set; }

        #endregion (Properties)

        private void SetUI(List<AdmlFile> excludeLanguages)
        {
            // ComboBox available Languages

            string allLanguages = Properties.Resources.LanguageCodes;
            string[] codes = allLanguages.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            List<string> excludeLanguagesCodes = new List<string>();

            foreach (AdmlFile admlFile in excludeLanguages)
            {
                excludeLanguagesCodes.Add(admlFile.Culture.Name);
            }

            foreach (string code in codes)
            {
                try
                {
                    if (!excludeLanguagesCodes.Contains(code))
                    {
                        culture newCulture = new culture(new System.Globalization.CultureInfo(code));
                        Action AddItems = () =>
                            {
                                this.cmbBxLanguages.Items.Add(newCulture);
                            };
                        this.Invoke(AddItems);
                    }
                }
                catch (Exception) { }
            }
            Action enableComboBox = () => { this.cmbBxLanguages.Enabled = true; this.cmbBxLanguages.Focus(); };
            this.Invoke(enableComboBox);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.SelectedCulture = (culture)this.cmbBxLanguages.SelectedItem;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void cmbBxLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnOk.Enabled = this.cmbBxLanguages.SelectedIndex != -1 && this.cmbBxLanguages.SelectedItem != null;
        }
    }
}
